package com.newer.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newer.domain.Apply;
import com.newer.domain.ApplyDetails;
import com.newer.domain.User;
import com.newer.dto.ApplyandDetailsDto;
import com.newer.dto.LoginDto;
import com.newer.dto.RequisitionDto;
import com.newer.dto.tableDataDto;
import com.newer.service.ApplyDetailsService;
import com.newer.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("requisition")
public class RequisitionContriller {

    @Autowired
    private ApplyService applyService;

    @Autowired
    private ApplyDetailsService applyDetailsService;

    @RequestMapping("selectApply")
    public Page<Apply> selectApply(int page, int size, String applyDate, Model model){
        Page page1=new Page();
        page1.setCurrent(page);
        page1.setSize(size);

        //获取订单采购表信息
        Page<Apply> applies=this.applyService.selectMaterial(page1,applyDate);



        model.addAttribute("applies",applies);

        return  applies;
     }

     @RequestMapping("insertApply")
     public Boolean insertApply(@RequestBody List<tableDataDto> tableData, HttpSession session){

        int userid=((LoginDto)session.getAttribute("login")).getUserId();
         Double Sumprice=0.0;
         int Sumcount=0;
         for (int i=0;i<tableData.toArray().length;i++){
              Sumprice += tableData.get(i).getPrice()*tableData.get(i).getCount();
              Sumcount += tableData.get(i).getCount();
         }
         System.out.println(Sumprice);
         System.out.println(Sumcount);

         Date myDate=new Date();
         SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
         String orderid=sdf.format(myDate);

        Apply apply=new Apply();
        apply.setApplyid(orderid);
        apply.setApplydate(new Date());
        apply.setUserid(userid);
        apply.setSumprice(Sumprice);
        apply.setSumcount(Sumcount);
            apply.setApprover(null);
        apply.setRemark(null);
        apply.setApplystate("申请中");



        Boolean apply1=this.applyService.save(apply);
        if(apply1){
            for (int i=0;i<tableData.toArray().length;i++) {
                ApplyDetails applyDetails = new ApplyDetails();
                applyDetails.setApplyid(apply.getApplyid());
                applyDetails.setApplydetailsid(null);
                applyDetails.setMaterialid(tableData.get(i).getMaterialid());
                applyDetails.setCount(tableData.get(i).getCount());

                applyDetails.setPrice((double) tableData.get(i).getPrice());

                this.applyDetailsService.save(applyDetails);
            }
            return true;
        }


        return false;
     }

     //查询采购申请明细信息
    @RequestMapping("selectRequisition")
    public Page<ApplyandDetailsDto> selectRequisition(int page,int size,String applyid){
        Page page1=new Page();
        page1.setCurrent(page);
        page1.setSize(size);



        return this.applyService.seleceApplyandDetails(page1,applyid);
    }



     //主管审核状态
    @RequestMapping("updateState")
    public Boolean updateState(String applyid,HttpSession session){

        String realname=((LoginDto)session.getAttribute("login")).getRealName();

        Apply apply=new Apply();
        apply.setApplystate("申请成功");
        apply.setApprover(realname);
        apply.setApplyid(applyid);
         this.applyService.updateById(apply);
        return true;
    }

     @RequestMapping("deleteDetails")
     public Boolean deleteDetails(String id){
            QueryWrapper<ApplyDetails> applyDetails=new QueryWrapper();
            applyDetails.eq("applyid",id);

            this.applyDetailsService.remove(applyDetails);

        return this.applyService.removeById(id);
     }

     //查订单明细表信息
     @RequestMapping("selectDetails")
    public List<ApplyDetails> selectDetails(String applyid){

        return this.applyDetailsService.selectApplyDetails(applyid);
     }


}
